package handler

import (
	"boy-go/middlewares"
	"boy-go/modules/demo/service"
	"boy-go/modules/demo/vo"
	"boy-go/pkg/request"
	"boy-go/pkg/response"
	"boy-go/pkg/xexcel"
	"boy-go/pkg/xnum"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/copier"
	"strings"
)

// AddTSchoolRouter 添加路由
func AddTSchoolRouter(group *gin.RouterGroup) {
	group.GET("/t_school/:id", middlewares.HasPermission("demo:t_school:query"), get_t_school)
	group.GET("/t_school/list", middlewares.HasPermission("demo:t_school:list"), list_t_school)
	group.POST("/t_school", middlewares.HasPermission("demo:t_school:add"), middlewares.Logger("学校表", middlewares.INSERT), add_t_school)
	group.PUT("/t_school", middlewares.HasPermission("demo:t_school:edit"), middlewares.Logger("学校表", middlewares.UPDATE), edit_t_school)
	group.POST("/t_school/export", middlewares.HasPermission("demo:t_school:export"), middlewares.Logger("学校表", middlewares.EXPORT), export_t_school)
	group.DELETE("/t_school/:ids", middlewares.HasPermission("demo:t_school:remove"), middlewares.Logger("学校表", middlewares.DELETE), del_t_school)
}

// get_t_school
// @Summary 获取学校表详情
// @Description 获取学校表详情（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Param id path string true "ID"
// @Success 200 {object} response.R[vo.TSchoolModel] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school/{id} [get]
func get_t_school(ctx *gin.Context) {
	var idStr = ctx.Param("id")
	id := xnum.Str2int64(idStr)
	m, err := service.NewTSchoolService().GetById(id)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxSuccess(ctx, m)
}

// list_t_school
// @Summary 获取学校表列表
// @Description 获取学校表列表（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.TSchoolModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school/list [get]
func list_t_school(ctx *gin.Context) {
	req := vo.QueryTSchoolReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	var pageReq = &request.PageRequest{
		PageNum:  req.PageNum,
		PageSize: req.PageSize,
	}
	page, err := service.NewTSchoolService().List(&req, pageReq)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	response.AjaxDataTable(ctx, page.List, page.Total)
}

// export_t_school
// @Summary 导出学校表列表
// @Description 导出学校表列表（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Success 200 {object} response.R[PageRes[vo.TSchoolModel]] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school/export [post]
func export_t_school(ctx *gin.Context) {
	req := vo.QueryTSchoolReq{}
	if err := ctx.BindQuery(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	req.Params = request.BindFormParams(ctx)
	page, err := service.NewTSchoolService().List(&req, nil)
	if err != nil {
		response.AjaxError(ctx, err)
		return
	}
	exports := make([]*vo.TSchoolExcelModel, 0)
	for _, m := range page.List {
		xv := &vo.TSchoolExcelModel{}
		err = copier.Copy(&xv, m)
		if err != nil {
			continue
		}
		exports = append(exports, xv)
	}
	excel := xexcel.NewXExcel[vo.TSchoolExcelModel]("学校表")
	excel.Export(ctx, exports)
}

// add_t_school
// @Summary 新增学校表
// @Description 新增学校表（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Param req body  vo.AddTSchoolReq true "req"
// @Success 200 {object} response.R[vo.AddTSchoolRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school [post]
func add_t_school(ctx *gin.Context) {
	var req vo.AddTSchoolReq
	if err := request.BindJSONParams(ctx, &req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewTSchoolService().Add(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// edit_t_school
// @Summary 编辑学校表
// @Description 编辑学校表（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Param req body  vo.EditTSchoolReq true "req"
// @Success 200 {object} response.R[vo.EditTSchoolRsp] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school [put]
func edit_t_school(ctx *gin.Context) {
	var req vo.EditTSchoolReq
	if err := ctx.ShouldBind(&req); err != nil {
		response.AjaxError(ctx, err)
		return
	}
	m, err := service.NewTSchoolService().Edit(&req)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, m)
	}
}

// del_t_school
// @Summary 删除学校表
// @Description 删除学校表（管理页面）
// @Tags demo-t_school
// @Accept json
// @Produce json
// @Success 200 {object} response.R[string] "ok"
// @Failure 400 {string} string "bad request"
// @Failure 500 {string} string "Internal Server Error"
// @Router /demo/t_school [delete]
func del_t_school(ctx *gin.Context) {
	var idsStr = ctx.Param("ids")
	ids := strings.Split(idsStr, ",")
	err := service.NewTSchoolService().BatchDel(ids)
	if err != nil {
		response.AjaxFailCode(ctx, response.ServerError)
	} else {
		response.AjaxSuccess(ctx, nil)
	}
}
